package com.yu.mall.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yu.mall.dto.BaseDTO;
import com.yu.mall.dto.ProductInfoDto;
import com.yu.mall.entity.ProductInfo;
import com.yu.mall.entity.User;
import com.yu.mall.entity.White;
import com.yu.mall.enums.StatusEnum;
import com.yu.mall.mapper.UserMapper;
import com.yu.mall.service.ProductService;
import com.yu.mall.service.UserService;
import com.yu.mall.service.WhiteService;
import com.yu.mall.util.ResultUtils;
import com.yu.mall.vo.ResultVO;
import com.yu.mall.vo.user.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
@Api(tags = "商品查询管理", value = "商品查询管理")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    
    @Autowired
    UserMapper userMapper;

    @Autowired
    WhiteService whiteService;
    
    @PostMapping("listQuery")
    @ApiOperation(value = "查询商品列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "nameWord", value = "商品名称，模糊查询", required = false, dataType = "String"),
            @ApiImplicitParam(name = "orderByType", value = "排序方式，默认排序不传,priceAsc:价格升序，priceDesc:价格降序", required = false, dataType = "String"),
            @ApiImplicitParam(name = "typeCode", value = "类型编号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "isLaobao", value = "是否是劳保，不传查所有", required = false, dataType = "boolean"),
            @ApiImplicitParam(name = "pageNum", value = "页码，默认1", required = false, dataType = "int",example  = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量，默认10", required = false, dataType = "int",example  = "10")})
    public ResultVO<List<ProductInfo>> listQuery(BaseDTO baseDTO,String nameWord, String orderByType, String typeCode, Boolean isLaobao,Integer pageNum, Integer pageSize ) {
        if (null == pageNum || pageNum < 1) {
            pageNum = 1;
        }
        if (null == pageSize || pageSize < 1 || pageSize > 100) {
            pageSize = 10;
        }
        ProductInfoDto reqDto = new ProductInfoDto();
        reqDto.setNameWord(nameWord);
        reqDto.setOrderByType(orderByType);
        log.info("typeCode======="+typeCode);
        reqDto.setTypeCode(typeCode);
        reqDto.setStatus(StatusEnum.ON.getCode());
        User user = userMapper.selectByPrimaryKey(baseDTO.getOpenid());
        if(user!=null) {
        	 White white = new White();
     		white.setPhone(user.getPhone());
     		List<White> select = whiteService.select(white);
             if(!"".equals(user.getIsCard()) && user.getIsCard() !=null) {
             	if(user.getIsCard().equals("1")) {
             		if(user.getPhone()!=null && !"".equals(user.getPhone())) {
             			if(select !=null && select.size()>0) {
             			}else {
             				reqDto.setIsLaobao(true);
             			}
             		}
             	}
             }
        }
       
//        if(null != isLaobao && isLaobao){
//        	if(select !=null && select.size()>0) {
//				reqDto.setIsLaobao(false);
//			}else {
//				 reqDto.setTypeCode(null);
//			}
//        }
        if ("9999".equals(reqDto.getTypeCode())) {
            reqDto.setTypeCode(null);
            reqDto.setIsLaobao(true);
        }
        try {
            List<ProductInfo> productInfoList = productService.pageQueryProduct(reqDto, pageNum, pageSize);
            for (ProductInfo productInfo : productInfoList) {
                productInfo.setPrice(null);
                productInfo.setRate(null);
            }
            log.info("productInfoList===="+JSONObject.toJSONString(productInfoList));
            return ResultUtils.success(productInfoList);
        } catch (Exception e) {
            log.error("查询商品列表，出错", e);
        }
        return ResultUtils.error("操作失败");
    }

    @GetMapping("getAllType")
    @ApiOperation(value = "查询商品所有分类")
    public ResultVO<JSONObject> getAllType(String openid) {
        JSONObject result = new JSONObject();
        JSONArray types = new JSONArray();
        Map<String, String> allProductType = productService.getAllProductType();
        for (Map.Entry<String, String> entry : allProductType.entrySet()) {
            JSONObject item = new JSONObject();
            item.put("id", entry.getKey());
            item.put("name", entry.getValue());
            types.add(item);
        }
        result.put("types",types);
        result.put("isLbUser",false);
        if(StrUtil.isNotBlank(openid)){
            ResultVO<UserVO> userVOResultVO = userService.get(openid);
            UserVO data = userVOResultVO.getData();
            if(null != data && "1".equals(data.getIsCard())){
                result.put("isLbUser",true);
            }
        }
        return ResultUtils.success(result);
    }

    @GetMapping("getDetail")
    @ApiOperation(value = "查询商品详情")
    @ApiImplicitParam(name = "skuId", value = "skuId", required = true, dataType = "String")
    public ResultVO<JSONObject> getDetail(String skuId) {
        if (StrUtil.isBlank(skuId)) {
            return ResultUtils.error("skuId为空");
        }
        try {
            JSONObject detail = productService.getProductDetail(skuId);
            return ResultUtils.success(detail);
        } catch (Exception e) {
            log.error("查询商品详情，出错", e);
        }
        return ResultUtils.error("操作失败");
    }
}
